package com.base.user.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mysteel.util.ObjectUtils;

import com.base.data.exception.DataException;
import com.base.user.dao.AdministratorDao;
import com.base.user.entity.Administrator;
import com.mysteel.page.Page;

@Service("administratorService")
public class AdministratorServiceImpl implements AdministratorService
{
	@Autowired
	private AdministratorDao administratorDao;
	
	@Override
	public void saveAdministrator(Administrator administrator)
	{
		if(ObjectUtils.notNull (administrator)){
			Map<String,Object> params =  new HashMap<String,Object>();
			
			params.put ("userName" ,administrator.getUserName ());
			
			List<Long> ids = administratorDao.query (params);
			for(long id:ids){
				if(ObjectUtils.notNull(administratorDao.get (id))){
					
					throw new DataException ("用户名已经存在,操作失败!");
				}
			}
			
			administrator.setCreateTime (System.currentTimeMillis ());
			administrator.setLastAccess (System.currentTimeMillis ());
			administrator.setSuperAdmin (false);
			administrator.setStatus (true);
			
			administratorDao.save (administrator);			
		}
		
	}

	@Override
	public void deleteAdministrator(long id)
	{
		
		administratorDao.delete (id);
	}

	@Override
	public void updateAdministrator(Administrator admin)
	{
		
		Administrator ad = administratorDao.get (admin.getId());
		
		if(ObjectUtils.notNull (ad))
		{
			if(ObjectUtils.notNull (admin.getUserName ()))
			{				
				ad.setUserName (admin.getUserName ());
			}
			if(ObjectUtils.notNull (admin.getPassword ()))
			{			
				ad.setPassword (admin.getPassword ());
			}
			if(ObjectUtils.notNull (admin.isSuperAdmin ()))
			{				
				ad.setSuperAdmin (admin.isSuperAdmin ());
			}
			if(ObjectUtils.notNull (admin.getName ()))
			{				
				ad.setName (admin.getName ());
			}
			if(admin.getAge ()!=0)
			{	
				ad.setAge (admin.getAge ());
			}
			if(ObjectUtils.notNull (admin.getGender ()))
			{
				ad.setGender (admin.getGender ());
			}
			if(ObjectUtils.notNull (admin.getMobile ()))
			{	
				ad.setMobile (admin.getMobile ());
			}
			if(ObjectUtils.notNull (admin.getEmail ()))
			{
				ad.setEmail (admin.getEmail ());
			}
			if(ObjectUtils.notNull (admin.getQxId ()))
			{
				ad.setQxId (admin.getQxId ());
			}
			if(ObjectUtils.notNull (admin.isStatus ()))		
			{			
				ad.setStatus (admin.isStatus ());
			}
			if(ObjectUtils.notNull  (admin.getLastAccess ()))
			{
				ad.setLastAccess (admin.getLastAccess ());
			}
			
			administratorDao.update (ad);
		}
	}

	@Override
	public Administrator getAdministrator(long id)
	{
		
		return administratorDao.get (id);
	}

	@Override
	public Page<Administrator> queryAdministrator(Map<String, Object> params, int currentPageNum,int maxPageRowCount)
	{
		
		Page<Administrator> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = administratorDao.count(params);

		if (count > 0)
		{
			page = new Page<Administrator>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> AdministratorIds = administratorDao.query(params);

			if (ObjectUtils.notEmpty(AdministratorIds))
			{
				List<Administrator> Administrators = new ArrayList<Administrator>(AdministratorIds.size());

				for (long AdministratorId : AdministratorIds)
				{
					Administrators.add(getAdministrator(AdministratorId));
				}

				page.setDatas(Administrators);
			}
		}

		return page;
	}
	
}
